<template>
<div>
<template>

<!-- 添加学生会隐藏端口 -->
<el-dialog title="添加学生会" :visible.sync="dialogFormVisible">
  <el-form :model="studentUnion">
    <el-form-item label="学生会姓名" label-width="120px">
      <el-input v-model="studentUnion.username" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="学生会密码" label-width="120px">
      <el-input v-model="studentUnion.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="学生会工号" label-width="120px">
      <el-input  v-model="studentUnion.tNo" autocomplete="off"></el-input>
    </el-form-item>
      <el-form-item label="学生会邮箱" label-width="120px">
      <el-input v-model="studentUnion.email" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureAddstudentUnion()">添加</el-button>
      <el-button size="small"  type="primary" @click="cancelAddstudentUnion()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 修改学生会隐藏端口 -->
    <el-dialog title="修改学生会" :visible.sync="updateDialogFormVisible">
  <el-form :model="ustudentUnion">
    <el-form-item label="学生会姓名" label-width="120px">
      <el-input v-model="ustudentUnion.username" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="学生会密码" label-width="120px">
      <el-input v-model="ustudentUnion.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="学生会工号" label-width="120px">
      <el-input  v-model="ustudentUnion.tNo" autocomplete="off"></el-input>
    </el-form-item>
      <el-form-item label="学生会邮箱" label-width="120px">
      <el-input v-model="ustudentUnion.email" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureUpdatestudentUnion()">修改</el-button>
      <el-button size="small"  type="primary" @click="cancelUpdatedstudentUnion()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 页面主体 -->
   <el-row :gutter="0">

     <!-- 添加学生会按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="addstudentUnion()">增加学生会</el-button>
    </el-col>

    <!-- 输入框 -->
    <el-col :span="6">
        <el-input
        v-model="studentUnionDTO.username"
        class="w-50 m-2"
        size="small"
        placeholder="请输入姓名"
        @input="search()"
        />
    </el-col>

    <!-- 搜索按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="search()">搜索</el-button>
    </el-col>


    <!-- 导出Excel -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="exportExcel()">导出Excel</el-button>
    </el-col>

  </el-row>

  <!-- 表格 -->
  <el-table :data="studentUnions" stripe border height="300" id="mytable">
    <el-table-column prop="id" sortable=true label="ID"/>
    <el-table-column prop="user.username" sortable=true label="昵称"/>
    <el-table-column prop="tNo" sortable=true label="学生会号码" />
    <el-table-column prop="user.email" sortable=true label="角色号码" />
        <el-table-column label="操作">
            <template #default="scope">
                <el-button size="small" @click="updatestudentUnion(scope.row)">
                   修改
                </el-button>
                <el-button size="small" type="danger" @click="deletestudentUnion(scope.row.id)">
                   删除
                </el-button>
            </template>
            </el-table-column>
  </el-table>

  <!-- 分页部分 -->
  <el-pagination
    small
    background
    layout="prev, pager, next"
    :page-size="studentUnionDTO.capacity"
    :current-page.sync="studentUnionDTO.index"
    @current-change="changeCurrent"
    :total="count"
    class="mt-4"
  />
</template>

</div>
</template>
<script>
export default {
  name: 'Home',
  data () {
    return {
      // 当前登录的用户角色
      role: '学生会',
      // 表格映射对象
      studentUnions: [],
      // 修改的对象
      ustudentUnion: {
        userJson: {
          '@type': 'com.nciae.po.User',
            username: '',
            password: '',
            email: '',
        },
            username: '',
            password: '',
            email: '',
            tNo: '',
      },
      // 表格查询对象
      studentUnionDTO: {
          index: 1,
          capacity: 10,
          username: ''
      },
      // 角色列表
      roles: [],
      // 添加学生会弹出控制
      dialogFormVisible: false,
      // 修改学生会弹出控制
      updateDialogFormVisible: false,
      // 添加学生会实体
      studentUnion: {
        username: '',
        password: '',
        email: '',
        userJson: {
          '@type': 'com.nciae.po.User',
            username: '',
            password: '',
            email: ''
        },
            username: '',
            password: '',
            email: '',
      },
      // 学生会列表总数
      count: 0,
      // 用户信息
      user: {},
      // 角色信息
      role: ''
    }
  },
  // 页面初始化函数 
  created () {

    // 获得角色信息
    this.role = window.sessionStorage.getItem('role')

    // 获得用户信息
    this.user = JSON.parse(window.sessionStorage.getItem('user'))

    this.$http({
        url: "/studentUnion",
        params: {
            username: this.studentUnionDTO.username
        }
    }).then(response => {
       this.studentUnions = response.data
    })
  },
  methods: {


    // 改变当前页
    changeCurrent() {
      this.findAll()
    },


    // 添加学生会
     addstudentUnion() {
       this.dialogFormVisible=true;
     },

     // 修改学生会成员
     updatestudentUnion(data) {
       this.ustudentUnion.id=data.user.id
       this.ustudentUnion.username=data.user.username
       this.ustudentUnion.password=data.user.password
       this.ustudentUnion.email=data.user.email
       this.ustudentUnion.tNo=data.tNo
       this.updateDialogFormVisible=true
     },

     // 删除学生会成员
     deletestudentUnion(id) {
        this.$http.delete('/studentUnion/' + id).then(response => {
          this.$message({
          message: '删除学生会成功',
          type: 'success'
          });
          this.findAll()
        })
     },
     ensureAddstudentUnion() {
      this.studentUnion.userJson.username=this.studentUnion.username
      this.studentUnion.userJson.password=this.studentUnion.password
      this.studentUnion.userJson.email=this.studentUnion.email
      this.studentUnion.userJson = JSON.stringify(this.studentUnion.userJson)
      this.$http.post("/studentUnion", this.studentUnion).then(response => {
        this.$message({
          message: '添加学生会成功',
          type: 'success'
        });
        this.clearstudentUnion()
        this.findAll()
      })
    },
    // 取消添加学生会
    cancelAddstudentUnion() {
      this.clearstudentUnion()
    },
    ensureUpdatestudentUnion() {
      this.ustudentUnion.userJson.username=this.ustudentUnion.username
      this.ustudentUnion.userJson.password=this.ustudentUnion.username
      this.ustudentUnion.userJson.email=this.ustudentUnion.email
      this.ustudentUnion.userJson = JSON.stringify(this.ustudentUnion.userJson)
      this.$http.put('/studentUnion', this.ustudentUnion).then(response => {
        this.$message({
          message: '修改学生会成功',
          type: 'success'
        });   
        this.updateDialogFormVisible=false
        this.findAll()
      })
    },
    cancelUpdatedstudentUnion() {
      this.updateDialogFormVisible=false
    },
    // 搜索对象，并映射
    search() {
      this.findAll()
    },
    // 查询所有，并映射
    findAll() {
      this.$http({
          url: "/studentUnion",
          params: {
              username: this.studentUnionDTO.username
          }
      }).then(response => {
        this.studentUnions = response.data
      })
    },
    // 清除学生会，并关闭窗口
    clearstudentUnion() {
        this.studentUnion.username=''
        this.studentUnion.password=''
        this.studentUnion.email=''
        this.studentUnion.tNo=''
        this.studentUnion.userJson = {'@type': 'com.nciae.po.User'}
        this.dialogFormVisible=false
     },

    // 导出Excel
    exportExcel () {
        var xlsxParam = { raw: true } 
        var wb = this.$xlsx.utils.table_to_book(document.querySelector('#mytable'), xlsxParam)
        /* get binary string as output */
        var wbout = this.$xlsx.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
        try {
            this.$filesaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), this.user.username + '导出学生会信息表.xlsx')
        } catch (e) {
        if (typeof console !== 'undefined') {
            console.log(e, wbout)
        }
        }
        return wbout
    }

  }
}
</script>
<style>

</style>